﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Xml;

namespace ReadDogv1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Title = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);
            WindowStartupLocation = WindowStartupLocation.CenterScreen;//窗体显示在正中间
            ViewModel.GetInstance()._mainWin = (MainWindow)Application.Current.MainWindow;

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ViewModel.GetInstance().log.DebugLog(-1, $"==========================================================================");
            ViewModel.GetInstance().log.DebugLog(-1, $"{Title}程序，启动成功");
            ViewModel.GetInstance().log.DebugLog(-1, $"烧录加密狗操作步骤如下：");
            ViewModel.GetInstance().log.DebugLog(-1, $"1、单击【更新烧录清单】按钮");
            ViewModel.GetInstance().log.DebugLog(-1, $"2、插入烧录母狗");
            ViewModel.GetInstance().log.DebugLog(-1, $"3、在未插入子狗的状态下，单击【开始烧录任务】按钮");
            ViewModel.GetInstance().log.DebugLog(-1, $"4、插入子狗，会自动按照烧录清单顺序烧录，烧录状态见【烧录任务状态】");
            ViewModel.GetInstance().log.DebugLog(-1, $"5、【烧录任务状态】提示 “ 烧录完成 ”，拔出子狗，插入下一个待烧录子狗");
            ViewModel.GetInstance().log.DebugLog(-1, $"==========================================================================");
            ViewModel.GetInstance().log.DebugLog(-1, $"检查加密狗操作步骤如下：");
            ViewModel.GetInstance().log.DebugLog(-1, $"1、单击【更新烧录清单】按钮");
            ViewModel.GetInstance().log.DebugLog(-1, $"2、单击【开始检查任务】");
            ViewModel.GetInstance().log.DebugLog(-1, $"3、插入子狗，会自动读取狗ID和三个激活码");
            ViewModel.GetInstance().log.DebugLog(-1, $"4、对比服务器注册信息，显示对应烧录任务并显示匹配");
            ViewModel.GetInstance().log.DebugLog(-1, $"==========================================================================");
         
            DisplayIPAddress();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ViewModel.GetInstance().log.DebugLog(-1, $"程序准备关闭，3秒倒计时开始");
            ViewModel.GetInstance().doglistData.Exit();
            System.Threading.Timer timer = new System.Threading.Timer((state) =>
            {
                // 使用 Dispatcher 来确保在 UI 线程上执行
                Application.Current.Dispatcher.Invoke(() =>
                {
                    // 关闭应用程序
                    // Application.Current.Shutdown();

                    // 或者强制关闭应用程序
                    Environment.Exit(0);
                });
            }, null, 3000, System.Threading.Timeout.Infinite);
        }
        private PerformanceCounter cpuCounter;
        private PerformanceCounter memoryCounter;

        private void DisplayIPAddress()
        {
            // 获取本机的网络接口
            NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface networkInterface in networkInterfaces)
            {
                // 过滤掉非操作状态的接口
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    // 获取IPv4地址
                    foreach (UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            // 将IP地址显示在Label控件上
                            Title += "_IP Address: " + ip.Address.ToString();
                            return; // 只显示第一个IPv4地址
                        }
                    }
                }
            }
        }
    }

    public class ViewModel
    {
        #region 建立单例

        private static ViewModel uniqueInstance;

        private static readonly object locker = new object();

        public static ViewModel GetInstance()
        {
            if (uniqueInstance == null)
            {
                lock (locker)
                {
                    if (uniqueInstance == null)
                    {
                        uniqueInstance = new ViewModel();
                    }
                }
            }
            return uniqueInstance;
        }

        #endregion

        public model_doglist doglistData = new model_doglist();
        public model_Log log = new model_Log();
        public MainWindow _mainWin;// 与前端界面建立联系，开始线程扫描
    }

}
